package com.shen.cas;

import java.util.concurrent.atomic.AtomicInteger;

public class Demo01 {

    //CAS  compareAndSet  比较并交换
    public static void main(String[] args) {
        AtomicInteger atomicInteger = new AtomicInteger(2020);//设定初始值为2020


        //expect:期望值
        //update：更新值
        //当满足期望值时set为更新值
        //会返回一个Boolean值 true为更新成功 false为更新失败
        System.out.println(atomicInteger.compareAndSet(2020, 2021));//true
        System.out.println(atomicInteger.get());//2021
        atomicInteger.getAndIncrement();//执行+1操作

        System.out.println(atomicInteger.compareAndSet(2020, 2022));//false
        System.out.println(atomicInteger.get());//2021

    }
}
